package com.zhp.ggweather;

import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity {
	
	private Fragment mContent;

	public static final int GET_WEATHER_SCUESS = 11;
	public static final int GET_WEATHER_FAIL = 10;
	public static final String UPDATE_WIDGET_WEATHER_ACTION = "com.zhp.action.update_weather";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if (findViewById(R.id.search_frame)==null) {
			setBehindContentView(R.layout.search_frame);
			getSlidingMenu().setSlidingEnabled(true);
			//设置滑动的屏幕范围为全屏区域可以滑动
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		}else{
			View v=new View(this);
			setBehindContentView(v);
			getSlidingMenu().setSaveEnabled(false);
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		}
		
		//设置Above View Fragment
		if (savedInstanceState!=null) {
			mContent=getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
		}
		
		if (mContent==null) {
			mContent=new WeatherFragment();
		}
		getSupportFragmentManager().beginTransaction().replace(R.id.weather_frame, mContent).commit();
		
		//设置Behind View Fragment
		getSupportFragmentManager().beginTransaction().replace(R.id.search_frame, new SearchFragment()).commit();
		
		SlidingMenu sm=getSlidingMenu();
		//设置SlidingMenu划出时主页面显示的剩余宽度
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeEnabled(true);
		//设置SlidingMenu滑动时的渐变程度
		sm.setFadeDegree(0.25f);
		sm.setBehindCanvasTransformer(new SlidingMenu.CanvasTransformer() {
			
			@Override
			public void transformCanvas(Canvas canvas, float percentOpen) {
				float scale = (float) (percentOpen * 0.25 + 0.75);
				canvas.scale(scale, scale, canvas.getWidth()/2, canvas.getHeight()/2);
			}
		});
		
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}
}
